﻿@model BBICMS.Model.ViewModel.Identity.UserListVM
@using BBICMS.Infrastructure.UI

@{
    var modelName = "用户";
    ViewBag.PageTitle = modelName;
    ViewBag.Title = ViewBag.PageTitle + "_安全";
}

@section header{
    <style>
        .table > thead > tr > th, .table > thead > tr > td, .table > tbody > tr > th, .table > tbody > tr > td, .table > tfoot > tr > th, .table > tfoot > tr > td{
            vertical-align:middle;
        }
    </style>

    }

<div class="main-content">

            <div class="panel panel-default">
                <header class="panel-heading">
                    <h3 class="panel-title">

                        <span class="fa fa-user fa-fw"></span> @ViewBag.PageTitle
                    </h3>
                </header>
                <div class="panel-body">   
                    @Ajax.RawActionLink("<i class='fa fa-plus fa-fw'></i>创建新用户",
                        "Create", "User",
                        new {},
                        new AjaxOptions { HttpMethod = "Get", UpdateTargetId = "create-container", OnComplete = "onAjaxCreateComplete" },
                        new { title = "创建新用户", @class = "btn btn-primary pull-right" })


                    @using (Html.BeginForm("Index", "User", FormMethod.Get, new { @class = "form-inline" }))
                    {
                        <div class="form-group">
                            <label class="sr-only" for="Username">用户名</label>
                            @Html.EditorFor(model => model.Username, new { htmlAttributes = new { @class = "form-control", placeholder = "用户名" } })
                        </div>
                        <div class="form-group">
                            <label class="sr-only" for="StartDate">开始日期</label>
                            @Html.EditorFor(model => model.StartDate, new { htmlAttributes = new { @class = "form-control", placeholder = "开始日期" } })
                        </div>
                        <div class="form-group">
                            <label class="sr-only" for="EndDate">结束日期</label>
                            @Html.EditorFor(model => model.EndDate, new { htmlAttributes = new { @class = "form-control", placeholder = "结束日期" } })
                        </div>
                            @*<div class="form-group">
                                    @Html.DropDownList("DepartmentId", null, "--所属部门--", htmlAttributes: new { @class = "form-control" })
                                </div>
                                <div class="form-group">
                                    @Html.DropDownList("PositionId", null, "--所在岗位--", htmlAttributes: new { @class = "form-control" })
                                </div>*@
                            <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span> 搜索</button>

                            }
                        </div>
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th></th>
                            <th>头像</th>
                            <th>
                                用户名
                            </th> 
                            <th>
                                邮箱
                            </th> 
                            <th>
                                姓名
                            </th>                           
                            <th>
                               手机
                            </th>
                                                 
                            <th>
                               激活？
                            </th>
                            <th>
                                创建日期
                            </th>                         
                          
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var item in Model.Users)
                        {
                           @Html.Partial("_UserOfList",item)
                        }
                    </tbody>
                </table>
                <div class="panel-footer text-center">
                    @Html.PagedListPager((IPagedList)ViewBag.OnePageOfUsers, page => Url.Action("Index",
           new { page, username = Model.StartDate, startDate = Model.StartDate, endDate = Model.EndDate}))
                </div>
            </div>
     
</div>

<div id="detail-container">
</div>
<div id="edit-container">
</div>
<div id="create-container">
</div>
<div id="role-container">
</div>
<div id="pwd-container">
</div>

<form id="anti-form" method="post">
    @Html.AntiForgeryToken()
</form>

@section Footer {

    @Scripts.Render("~/bundles/jqueryvalWithBootstrap")
 
<script src="~/Content/bootstrap/plugins/bootbox/bootbox.min.js"></script>

    <script>
    

        function OnSuccessLink(data) {
            Common.ShowBox(data.Status,data.Message,"操作")
        }

        function onAjaxComplete() {
            $('#ModalUserDetail').modal();
        }

        function onBeginSave() {
            $("#btnSave i").removeClass("fa-save").addClass("fa-spinner fa-spin")
        }
        function onCompleteSave() {
            $("#btnSave i").removeClass("fa-spinner fa-spin").addClass("fa-save")
        }


        //========== create user ===========
        function onAjaxCreateComplete() {
            $('#ModalUserCreate').modal();
        }
        function updateSuccessCreate(data) {

            switch (data.Status) {            
                case 2:
                    toastr.error(data.Message, '@modelName');
                    break;
                case 1:
                    toastr.success("操作成功", '@modelName');
                    $('#ModalUserCreate').modal('hide');
                    setTimeout(function () {
                        location.reload();
                    }, 1000);
                    break;
                case 4:
                    toastr.warning(data.Message, modelName)
                    break;
            }

        }

        //========== end create user ===========

        //========== edit user ===========
        function onAjaxEditComplete() {
            $('#ModalUserForm').modal();

            $.validator.addMethod('qq', function (value, element) {
                if (value.length > 0)
                    return /^[1-9][0-9]{4,9}$/.test(value)
                return true;
            });
            $.validator.unobtrusive.adapters.addBool("qq");

            $.validator.addMethod('chinamobile', function (value, element) {
                if (value.length > 0)
                    return /^0{0,1}(13[0-9]|15[7-9]|153|156|170|178|18[0-9])[0-9]{8}$/.test(value)
                return true;
            });

            $.validator.unobtrusive.adapters.addBool("chinamobile");
        }
                
      
        function updateSuccess(data) {

            switch (data.Status) {              
                case 2:
                    toastr.error(data.Message, '@modelName');
                    break;
                case 1:
                    toastr.success("操作成功", '@modelName');
                    $('#ModalUserForm').modal('hide');
                    setTimeout(function () {
                        location.reload();
                    }, 1000);
                    break;
                case 4:
                    toastr.warning(data.Message, modelName)
                    break;
            }

        }
        //========== end edit user ===========

        
        //========== edit user ===========
        function onAjaxPwdComplete() {
            $('#ModalUserPwd').modal();

        }
        function SetPasswordSuccess(data) {
            switch (data.Status) {
                case 2:
                    toastr.error(data.Message, '@modelName');
                    break;
                case 1:
                    toastr.success("操作成功", '@modelName');
                    $('#ModalUserPwd').modal('hide');
                    break;
                case 4:
                    toastr.warning(data.Message, modelName)
                    break;
            }
        }
        //========== end edit user ===========
        //========== set role ===========
        function onAjaxRoleComplete(){
            $('#ModalUserRole').modal();

        }

        function updateSuccessRole(data) {
            switch (data.Status) {
                case 2:
                    toastr.error(data.Message, '@modelName');
                    break;
                case 1:
                    toastr.success("操作成功", '@modelName');
                    $('#ModalUserRole').modal('hide');
                    break;
                case 4:
                    toastr.warning(data.Message, modelName)
                    break;
            }
        }
        
  
        //========== end role ===========

        $(function () {
          
      

            //确认框默认语言
            bootbox.setDefaults({
                locale: "zh_CN"
            });

            //删除数据
            $('.delete-item').click(function (e) {
                e.preventDefault();
                var url = $(this).attr('data-url'), that = $(this), title = '@string.Format("删除{0}",EntityNames.Category)';

                bootbox.setLocale("zh_CN");

                bootbox.confirm("是否确定删除该项？", function (result) {
                    if (result) {
                        Common.DeleteItem(url, title, that);


                    }
                });
            });

            //删除数据
            //$('.delete-item').click(function (e) {
            //    e.preventDefault();

            //    var url = $(this).attr('data-url');
            //    var that = $(this);
            //    bootbox.confirm("你确定要删除此项吗？", function (result) {
            //        /*DELETE*/
            //        if (result) {
            //            $.ajax({
            //                url: url,
            //                dataType: "json",
            //                type: "POST",
            //                contentType: 'application/json; charset=utf-8',
            //                data: JSON.stringify({}),
            //                async: true,
            //                processData: false,
            //                cache: false,
            //                success: function (data) {

            //                    switch (data.Status) {
            //                        case 2:
            //                            toastr.error(data.Message, modelName);
            //                            break;
            //                        case 1:
            //                            toastr.success(data.Message, modelName);
            //                            that.closest('tr').remove();
            //                            break;
            //                        case 4:
            //                            toastr.warning(data.Message, modelName)
            //                            break;
            //                    }                              
                             
            //                },
            //                error: function (xhr) {
            //                    toastr.error(modelName + '删除失败！', '删除' + modelName);
            //                }
            //            });
            //        }

            //    });


            //});


            //锁定 激活
            $('.active-item').click(function (e) {

                e.preventDefault();

                var actionName = $(this).attr('data-original-title');

                var url = $(this).attr('data-url');
                var that = $(this);

                        $.ajax({
                            url: url,
                            dataType: "json",
                            type: "POST",
                            contentType: 'application/json; charset=utf-8',
                            data: JSON.stringify({}),
                            async: true,
                            processData: false,
                            cache: false,
                            success: function (data) {
                                switch (data.Status) {
                                    case 2:
                                        toastr.error(data.Message, modelName);
                                        break;
                                    case 1:
                                        toastr.success(data.Message, modelName);
                                        setTimeout(function () {
                                            location.reload();
                                        }, 1000);
                                        break;
                                    case 4:
                                        toastr.warning(data.Message, modelName)
                                        break;
                                }                               
                            },
                            error: function (xhr) {
                                toastr.error(modelName + '失败！', modelName);
                            }
                        });

            });

        });
    </script>
}